<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @refresh-change="refreshChange"
               @selection-change="selectionChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.rtcseat_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  // import {getTree, getDetail, add, update, remove} from "@/api/bureau/miniaccident/rtcseat";
  import {getRoomAlive} from "@/api/bureau/monitor/alive"
  import {mapGetters} from "vuex";
  import {baseUrl} from "../../../config/env";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "坐席编号",
              prop: "roomId",
              // type: "number",
              // rules: [{
              //   required: true,
              //   message: "请输入序号",
              //   trigger: "blur"
              // }]
            },
            {
              label: "坐席名称",
              prop: "roomType",
              // rules: [{
              //   required: true,
              //   message: "请输入坐席名称",
              //   trigger: "blur"
              // }]
            },
            {
              label: "上级",
              prop: "createrId",
              // type: "tree",
              // dicData: [],
              // slot: true,
              // props: {
              //   label: "seatName",
              //   value: "id",
              //   children: "children"
              // }
            },
            {
              label: "应用类型",
              prop: "createrName",
              // type: "select",
              // dicUrl: baseUrl + "/api-rtc/seat/typeList",
              // props: {
              //   label: "name",
              //   value: "typeId"
              // },
              rules: [{
                required: true,
                message: "请输入接警类型",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "receiverId",
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "排序",
              prop: "receiverName",
              // type: "number",
              rules: [{
                required: true,
                message: "请输入序号",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          // addBtn: this.vaildData(this.permission.rtcseat_add, false),
          // viewBtn: this.vaildData(this.permission.rtcseat_view, false),
          // delBtn: this.vaildData(this.permission.rtcseat_delete, false),
          // editBtn: this.vaildData(this.permission.rtcseat_edit, false)
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn:  false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        if(row.parentId == ""){
          row.parentId = "-1";
        }
        add(row).then(() => {
          done();
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        if(row.parentId == ""){
          row.parentId = "-1";
        }
        update(row).then(() => {
          done();
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad();
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad();
      },
      searchChange(params, done) {
        this.query = params;
        this.onLoad(params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      refreshChange() {
        this.onLoad();
      },
      onLoad() {
        this.loading = true;
        getRoomAlive().then(res => {
          this.data = res.data.data;
          this.loading = false;

        })
        // getTree(this.query).then(res => {
        //   this.data = res.data.data;
        //   const column = this.findObject(this.option.column, "parentId");
        //   column.dicData = res.data.data;
        //   this.loading = false;
        //   this.selectionClear();
        // });
      }
    }
  };
</script>

<style>
</style>
